<?php
namespace Admin\Controller;
use Think\Controller;
class phoneController extends Controller {
    // 后台登录页
    public function index(){
        if(isset($_SESSION['admin'])){
            $phone = D('phone');
            $phoneList = $phone->order('create_at desc,id desc')->select();
            $this->assign('phoneList',$phoneList);
            $this->display();
            
        }else{
            $url = 'http://localhost/phone-order/index.php/Admin/Index/manage';
            header("location: $url");
        }
    }

 // 商品添加
    public function phoneAdd(){
        $name = $_POST['name'];
        $img = $_POST['img'];       
        $info = $_POST['info'];
        $productdate = $_POST['productdate'];
        $count = $_POST['count'];
        $type = $_POST['type'];
        $price = $_POST['price'];
        $discountprice = $_POST['discountprice'];
        if(!$name || !$img || !$productdate || !$info || !$count ||!$price|| !$discountprice){
            $result = array(
                'code'=> '1',
                'ext'=> '缺少参数'
            );
            echo json_encode($result,JSON_UNESCAPED_UNICODE);
            return;
        }
        $phone = D('phone');
        $data['name'] = $name;
        $data['img'] = $img;
        $data['info'] = $info;
        $data['productdate'] = $productdate;
        $data['count'] = $count;
        $data['type'] = $type;
        $data['price'] = $price;
        $data['discountprice'] = $discountprice;
        $data['create_at'] = date('Y-m-d H:i:s');
        $data['update_at'] = $data['create_at'];
        if($phone->create($data)){
            $id = $phone->add();
            if($id){
                $phone_template = $phone->where("id='$id'")->find();
                $result = array(
                    'code' => '0',
                    'ext' => 'success',
                    'obj' => $phone_template
                );
                //echo json_encode($result,JSON_UNESCAPED_UNICODE);
                echo json_encode($result);
            }
        }
    }

 
    //商品删除
    public function phoneDelete(){
        $id = $_POST['id'];
        $phone = D('phone');
        $data['state'] = 1;        
        //$success = $project->where("id='$id'")->delete();
        $success = $phone->where("id='$id'")->delete($data);
        if($success){
            $result = array(
                'code'=> '0',
                'ext'=> 'success'
            );
            echo json_encode($result);
        }else {
            $result = array(
                'code'=> '1',
                'ext'=> 'fail'
            );
            echo json_encode($result);
        }
    }
    //刷列表
    public function phoneList(){
        $phone = D('phone');
        $resultArr = $phone->select();
        //echo json_encode($resultArr,JSON_UNESCAPED_UNICODE);
        echo json_encode($result);
    }
    //商品编辑
    public function phoneEdit($id,$name,$img,$info,$productdate,$discountprice,$count,$type,$price){
        $phone = D('phone');
        $data['name'] = $name;
        $data['img'] = $img;
        $data['info'] = $info;
        $data['productdate'] = $productdate;
        $data['count'] = $count;
        $data['type'] = $type;
        $data['price'] = $price;
        $data['discountprice'] = $discountprice;
        $data['create_at'] = date('Y-m-d H:i:s');
        $data['update_at'] = $data['create_at'];
        $success = $phone->where("id='$id'")->save($data);
        if($success){
            $phone_template = $phone->where("id='$id'")->find();
            $result = array(
                'code'=> '0',
                'ext'=> 'success',
                'obj'=>$phone_template
            );
            echo json_encode($result);
        }else {
            $result = array(
                'code'=> '1',
                'ext'=> 'fail'
            );
            echo json_encode($result);
        } 
    } 
}
